<?php
class SlidesController extends CController {
	public $defaultAction = 'edit';

	public function filters() {
		return array(
				'accessControl'
		);
	}
	
	
	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}
	
	public function actionEdit() {
		if(isPost()) {
			$slide = Slides::model()->findbyPk($_POST['Slides']['slideId'] !== null ? (int)$_POST['Slides']['slideId'] : -1);
			
			$slide->attributes = $_POST['Slides'];
			// Upload file and output the image as file
			$this->picProcess($slide);
			// Update the data
			$slide->save();
		}
		
		$slides = Slides::model()->getAll();
		$_slide = new Slides;
		
		$this->render('slidesUpdate', array(
				'slides' => $slides,
				'_slide' => $_slide
			)
		);
	}
	
	public function actionCreate() {
		if(isPost()) {
			$slide = new Slides;
			$slide->attributes = $_POST['Slides'];
			$slide->setAttribute('slideOrder', (int)$_POST['Slides']['slideOrder']);
			if(isset($_POST['Slides']['uploadedPic']) != '' && $slide->save()) {
				if($this->picProcess($slide, true)) {
					$message = '轮换图片添加成功。';
					Yii::app()->user->setFlash('message', $message);
				}
			} else {
				$error = '图片添加失败，请检查必填项目。';
				Yii::app()->user->setFlash('error', $error);
			}
			$this->redirect(array('/admin/slides/'));
		}
	}
	
	public function actionDelete() {
            $slideId = isset($_GET['id']) ? (int)$_GET['id'] : null;
            if($slideId !== null) {
            	$slide = Slides::model()->findByPk($slideId);
				if($slide !== null) {
					if($slide->delete()) {
                    	$message = '轮换图片删除成功。';
                    	@unlink(Yii::app()->basePath.'/../uploads/slides/'.$slide->slidePic);
                    	Yii::app()->user->setFlash('message', $message);
                        $this->redirect(Yii::app()->request->urlReferrer);
                    }
				}
            }
	}
	
	private function picProcess(&$slide, $save = false, $ctrl = 'uploadedPic') {
		// Upload file now
		$uploadedPic = CUploadedFile::getInstance($slide, $ctrl);
		if(is_uploaded_file(isset($uploadedPic->tempName) ? $uploadedPic->tempName : '')) {
			$tmpPath = UPLOADS_TEMP.'/'.randStr();
			if($uploadedPic->saveAs($tmpPath)) {
				// Crop the image and save as file
				Yii::import('application.extensions.image.Image');
				$image = new Image($tmpPath);
				$ext = '.jpg';
				$slidePic = $slide->slideId.'_'.time().$ext;
				$savePath = UPLOADS_ANIM.'/'.$slidePic;
				$flag = $image->crop(
					Yii::app()->params->slideWidth, 
					Yii::app()->params->slideHeight
				)->save($savePath);
				// Circle the image's corner
				$tmpSrc = imagecreatefromjpeg($savePath);
				$tplSrc = imagecreatefrompng('images/circle_corner.png');
				imagesavealpha($tplSrc, true);
				imagecopy($tmpSrc, $tplSrc, 0, 0, 0, 0, Yii::app()->params->slideWidth, Yii::app()->params->slideHeight);
				imagejpeg($tmpSrc, $savePath);
				
				$slide->slidePic = $flag ? $slidePic : $slide->slidePic;
				if($save) {
					return $slide->save();
				}
			}
		}
	}
}